package com.chen.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;
import com.chen.springboot.common.Result;


import com.chen.springboot.domain.Team;
import com.chen.springboot.entity.Poster;
import com.chen.springboot.mapper.TeamMapper;
import com.chen.springboot.service.impl.TeamServiceImpl;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonProcessingException;

import io.swagger.annotations.Api;


import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

import static java.lang.Integer.parseInt;


/**
 * @author chenchao
 * @date 2022/6/18 16:24
 */
@RestController
@Api(value = "TeamController", tags = "球队情况")
@RequestMapping("/team")
@Slf4j
public class TeamController {

    @Resource
    private TeamServiceImpl teamService;
    public static String s = null;


    @GetMapping("/findAll")
    @ApiOperation("查询所有的球队数据")
    public Result findAll(){
        return Result.success(teamService.list());
    }



    @RequestMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result selectDict(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestParam("") String displayAbbr,
                             @RequestParam("")   String displayConference,
                             @RequestParam("") String division,
                             @RequestParam("") String city){

        return Result.success(teamService.selectPage(pageNum, pageSize,displayAbbr,displayConference,division,city));
    }


    @PutMapping("/updateTeam")
    @ApiOperation(value = "更新")
    public Result updateTeam(@RequestBody Team team){

        return Result.success(teamService.updateById(team));

    }

    @DeleteMapping("/deleteTeam/{id}")
    public Result deleteTeam(@PathVariable("id") int id){
        return Result.success(teamService.removeById(id));
    }


    @PostMapping("/addTeam")
    @ApiOperation(value = "添加")
    public Result addTeam(@RequestBody Team team){

        return Result.success(teamService.save(team));

    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(teamService.removeByIds(ids));
    }



    @GetMapping("/pachou")
    @ApiOperation("爬虫")
    public String teamMessage()  {

//        s = HttpUtil.get("https://china.nba.cn/stats2/season/conferencestanding.json?");
        s = HttpUtil.get("https://china.nba.cn/stats2/league/playerlist.json");

        return s;
    }

    @PostMapping("/addBatch2")
    @ApiOperation(value = "批量插入")
    public Result addBatch2(@RequestBody String str) throws IOException, InterruptedException {

        JSONArray jsonArray = JSONArray.parseArray(str);
        FileOutputStream outputStream =null;
//        FileInputStream inputStream =null;
        InputStream inputStream=null;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(i).toString());
            String playerProfile = jsonObject.getString("playerProfile");
            String teamProfile = jsonObject.getString("teamProfile");
            JSONObject object = JSONObject.parseObject(playerProfile);
            JSONObject object2 = JSONObject.parseObject(teamProfile);
//            System.out.println(object.getString("displayName"));
//            System.out.println(object2.getString("city"));
            String playerImg = object.getString("playerId");
            String displayName = object.getString("displayName");
            System.out.println(playerImg);
        }

        return Result.success();
    }



    @PostMapping("/addBatch")
    @ApiOperation(value = "批量插入")
    public Result addBatch(@RequestBody String str) {


        JSONArray jsonArray = JSONArray.parseArray(str);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = JSONObject.parseObject(jsonArray.get(i).toString());
            String profile = json.getString("profile");
            JSONObject jsons = JSONObject.parseObject(profile);
            String city = jsons.getString("city");
            String abbr = jsons.getString("abbr");
            String cityEn = jsons.getString("cityEn");
            String displayConference = jsons.getString("displayConference");
            String displayAbbr = jsons.getString("displayAbbr");
            String nameEn = jsons.getString("nameEn");
            String division = jsons.getString("division");


            Team team = new Team();
            team.setCity(city);
            team.setAbbr( abbr);
            team.setCityEn(cityEn);
//            team.setCreateTime(new Date());
            team.setDisplayConference(displayConference);
            team.setDisplayAbbr(displayAbbr);
            team.setNameEn(nameEn);
            team.setDivision(division);
            teamService.save(team);
        }


        return Result.success("成功");
    }

    @GetMapping("/demo")
    @ApiOperation(value = "nba.json解析")
    public Result batchMessage() throws Exception {
        File jsonfile = ResourceUtils.getFile("classpath:nba.json");  //通过Java自带的读取工具对文件目录下的文件进行读取
        String jsonsting = FileUtils.readFileToString(jsonfile);      //读取到的文件转换成为String类型
        JSONArray array = JSONArray.parseArray(jsonsting);             //根据原本的类型转换为json数组
//        Map<String,Object> maps = new TreeMap<>();                     //采用treemap的原因是想让数据有序，免得数据库里面的数据杂乱无章


        for (int i = 0; i < array.size(); i++) {                              //遍历循环，根据不同的json文件采用不同的方式
            JSONObject jsons = JSONObject.parseObject(array.get(i).toString());     //遍历取
            String city = jsons.getString("city");
            String abbr = jsons.getString("abbr");
            String cityEn = jsons.getString("cityEn");
            String displayConference = jsons.getString("displayConference");
            String displayAbbr = jsons.getString("displayAbbr");
            String nameEn = jsons.getString("nameEn");
            String division = jsons.getString("division");


/*            Team team = new Team();
            team.setAbbr("/" + abbr);
            team.setCityEn(cityEn);
//            team.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd hh:mm:ss"));
            team.setDisplayConference(displayConference);
            team.setDisplayAbbr(displayAbbr);
            team.setNameEn(nameEn);
            team.setDivision(division);*/
            //遍历存
//            teamService.save(team);
//            System.out.println(jsons.toJSONString());
        }

        //int counts = commonCityMapper.insertBatch(maps);

        return Result.success("插入成功");
    }





}
